<template>
  <div v-loading="loading">
    <el-form >
      <el-form-item>
        <div class="div-form-text">
          <el-input type="textarea" v-model="followUpContent" placeholder="请输入跟进内容，至少20个字" show-word-limit></el-input>
          <el-button type="primary" style="margin: 15px 0" @click="clickPublishFollowUpContent">发布</el-button>
        </div>
      </el-form-item>
    </el-form>
    <div class="log-record" v-for="record in this.followUpRecordsList">
      <div class="log-record-text">
        <div class="log-record-text-mg" v-if="record.updateAbleFlag"><span class="log-operation-time">{{unFollowDayAmount}}</span>天未跟进</div>
        <div class="log-record-text-mg"><span class="log-text-name">{{record.createByName}}</span></div>
        <div class="log-record-text-mg"><span class="log-text-content">{{record.content}}</span></div>
        <div class="log-record-text-mg"><span class="log-text-time">{{record.createTime}}</span></div>
      </div>
      <div class="log-record-operation">
        <el-button type="danger" @click="clickDelete(record)" v-if="record.deleteAbleFlag" plain>删除</el-button>
        <el-button @click="showAlertRecordDialog(record)" v-if="record.updateAbleFlag" plain>修改</el-button>
      </div>
    </div>
    <el-dialog
      title=""
      :visible.sync="alertRecordDialogVisible"
      width="50%">
      <div slot="title"><h3>跟进记录修改</h3></div>
      <div>
        <el-form label-width="70px" >
          <el-form-item label="修改内容"  >
            <el-input v-model="updateFollowUpContent" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" show-word-limit></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="alertRecordDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="clickUpdateFollowUpContent">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getFollowUpRecordsList,
  getUnfollowDayAmount,
  saveFollowUpContent,
  updateFollowUpContent,
  deleteFollowUpRecord
} from '../../../../api/customer/followup'
import {getUserInfo} from '../../../../api/sys'

export default {
  name: 'regional-list-details-follow-up',
  props: {
    id: {
      type: Number,
      default: null
    }
  },
  created () {
    this.getUserInfoReq()
    this.getUnfollowDayAmount()
    this.getFollowUpRecordsList()
  },
  data () {
    return {
      loading: false,
      alertRecordDialogVisible: false,
      recordId: '',
      followUpContent: '',
      updateFollowUpContent: '',
      unFollowDayAmount: '',
      followUpRecordsList: [],
      userInfo: {}
    }
  },
  methods: {
    getUserInfoReq () {
      getUserInfo((user) => { this.userInfo = user })
    },
    showAlertRecordDialog (record) {
      this.updateFollowUpContent = record.content
      this.recordId = record.id
      this.alertRecordDialogVisible = true
    },
    // 获取未跟进天数
    getUnfollowDayAmount () {
      getUnfollowDayAmount(this.id, (data) => { this.unFollowDayAmount = data.data.unFollowDayAmount })
    },
    // 修改发布内容
    clickUpdateFollowUpContent () {
      this.loading = true
      updateFollowUpContent(
        {
          'id': this.recordId,
          'content': this.updateFollowUpContent
        },
        () => {
          this.getFollowUpRecordsList()
          this.loading = false
          this.alertRecordDialogVisible = false
        },
        () => {
          this.loading = false
        }
      )
    },
    // 点击发布内容
    clickPublishFollowUpContent () {
      let contentLen = this.followUpContent.trim().length
      if (contentLen >= 20) {
        this.loading = true
        saveFollowUpContent(
          {
            'customerId': this.id,
            'content': this.followUpContent
          },
          () => {
            this.getFollowUpRecordsList()
            this.followUpContent = ''
            this.loading = false
          },
          () => {
            this.loading = false
          }
        )
      } else {
        this.$message.warning(`请输入至少20个字,目前字数为${contentLen},还需${20 - contentLen}`)
      }
    },
    getFollowUpRecordsList () {
      if (this.id) {
        this.loading = true
        getFollowUpRecordsList(this.id,
          (data) => {
            this.followUpRecordsList = data.data
            this.loading = false
          }, () => {
            this.loading = false
          })
      }
    },
    // 删除记录
    clickDelete (record) {
      this.$confirm('确定删除该条记录？', '提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteFollowUpRecord(record.id,
          () => {
            this.$message.success('删除成功!')
            this.getFollowUpRecordsList()
          })
        })
    }
  }
}
</script>

<style scoped>

.div-form-text{
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}
.log-record{
  display: flex;
  justify-content: space-between;
  border-bottom: 1px dashed  #EBEEF5;
  padding: 15px 0;
}
.log-record-text{
  display: flex;
  flex-direction: column;

}

.log-record-operation{
  display: flex;
  align-items: center;
  // flex-direction: column;
  // justify-content: flex-end;
}


.log-text-name{
    font-size: 16px;
    line-height: 17px;
    font-weight: 500;
}

.log-text-content{

    font-weight: 400;
    line-height: 17px;
    font-size: 14px;

}

.log-text-time{

    font-size: 13px;
    font-weight: 400;
    line-height: 17px;
    color: #8C8C8C;

}

.log-operation-time{

    font-weight: 400;
    line-height: 17px;
    color: #E96E6E;

}

.log-record-text-mg{
  margin-bottom: 5px;
}

.log-record-operation-mg{
  margin-bottom: 5px;
}
</style>
